# -*- coding: utf-8 -*-
# @Product Name: 
# @Author  :    wyp
# @Time    :    2022/1/5 下午2:47
# @Description:

from flask import Flask, render_template, request, url_for, flash, redirect
import sqlite3

app = Flask(__name__)
app.config['SECRET_KEY'] = 'this is wyp de test,444'

def get_db_conn():
    conn = sqlite3.connect('database.db')
    conn.row_factory = sqlite3.Row  # 将数据变为字典
    return conn

def get_post(post_id):
    conn = get_db_conn()
    post = conn.execute('select * from posts where id = ?',(post_id,)).fetchone()
    return post

@app.route('/')
def index():
    # return render_template('index.html')
    conn = get_db_conn()
    posts = conn.execute('select * from posts order by created desc ').fetchall()
    return render_template('index.html', posts=posts)

@app.route('/posts/<int:post_id>')
def post(post_id):
    post = get_post(post_id)
    return render_template('post.html',post=post)

@app.route('/posts/new', methods=('GET','POST'))
def new():
    if request.method == 'POST':
        title = request.form('title')
        content = request.form['content']

        if not title:
            flash('标题不能为空')
        elif not content:
            flash('内容不能为空')
        else:
            conn = get_db_conn()
            conn.execute('insert int posts (title, content) values (?,?)',(title,content))
            conn.commit()
            conn.close()
            flash('文章保存成功')
            return redirect(url_for('index'))

    return render_template('new.html')

@app.route('/posts/<int:post_id>/edit',methods=('GET','POST'))
def edit(post_id):
    post = get_post(post_id)

    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']

        if not title:
            flash('标题不能为空')
        else:
            conn = get_db_conn()
            conn.execute('update posts set title = ?, content = ?'
                         'where id = ?',
                         (title, content, post_id))
            conn.commit()
            conn.close()
            return redirect(url_for('index'))

    return render_template('edit.html', post=post)


@app.route('/posts/<int:post_id>/delete',methods=('POST',))
def delete(post_id):
    post = get_post(post_id)
    conn = get_db_conn()
    conn.execute('delete from posts where id = ?',(post_id,))
    conn.execute()
    conn.close()
    flash('"{}" 删除成功！'.format(post['title']))
    return redirect(url_for('index'))

@app.route('/posts/about')
def about():
    return render_template('about.html')